<?php
/*
 * @Author: Wulala
 * @Date: 2025-04-02 17:15:44
 * @LastEditTime: 2025-04-15 10:20:48
 * @LastEditors: Wulala
 * @Description: 
 * @FilePath: \yoshop\app\common\service\ExchangeService.php
 * 可以输入预定的版权声明、个性签名、空行等
 */
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\common\service;

use GuzzleHttp\Client;

/**
 * 积分兑换平台服务类
 * Class ThirdService
 * @package app\store\service
 */
class ExchangeService extends BaseService
{
    public $url;
    public $app_secret;

    public function __construct()
    {
         $this->url=env('exchange.url');
         $this->app_secret=env('appsecret.app_secret');
    }
 
    // 根据appid获取app信息
    public static function getAppInfo($appid)
    {
        $instance = new self();
        $url= $instance->url;
        $method="/api/Notify/getAppInfo";
        $url= $url.$method;

        $params['appid']=$appid;

        $res=self::http_get($url,$params);
     
        return json_decode($res, true);
    }
    
    // 商品下单
    public static function placeOrder($data)
    {
        $instance = new self();
        $url= $instance->url;
        $method="/api/order.order/placeOrder";
        $url= $url.$method;

        $params['appid']=$data['appid'];
        $params['shop_order_no']=$data['shop_order_no'];
        $params['app_user_id']=$data['app_user_id'];
        $params['app_user_name']=$data['app_user_name'];
        $params['sent_to_user']=$data['sent_to_user'];
        $params['sent_to_address']=$data['sent_to_address'];
        $params['sent_to_tel']=$data['sent_to_tel'];
        $params['goods_arr']=$data['goods_arr'];

        $res=self::http_post($url,$params);

        return json_decode($res,true);
    }


     // usdt/paypal下单
     public static function placeCash($data)
     {
         $instance = new self();
         $url= $instance->url;
         $method="/api/order.order/placeCash";
         $url= $url.$method;

         $params['shop_order_no']=$data['shop_order_no'];
         $params['address']=$data['address'];
         $params['appid']=$data['appid'];
         $params['money_type']=$data['money_type'];
         $params['price']=$data['price'];

         $params['app_user_id']=$data['app_user_id'];
         $params['app_user_name']=$data['app_user_name'];

         $res=self::http_post($url,$params);
 
         return json_decode($res,true);
     }

    // post请求
    public static function http_post($url,$params){
        $instance = new self();
        $app_secret=$instance->app_secret;
        $params['timestamp']=time()*1000;
        $sign=self::generateSign($params, $app_secret);

        $params['sign']=$sign;

        $client = new Client();
        $response = $client->request('POST', $url, [
            'form_params' => $params
        ]);
        $body = $response->getBody()->getContents();

        return $body;
    }

    // get请求
    public static function http_get($url,$params){
        $instance = new self();
        $app_secret=$instance->app_secret;
        $params['timestamp']=time()*1000;
        $sign=self::generateSign($params, $app_secret);

        $params['sign']=$sign;

        $client = new Client(['verify' => false]);
        $response = $client->get($url.'?'.http_build_query($params));
        $body = $response->getBody()->getContents();
       
        return $body;
    }


     /**
     * 生成签名
     * @param array $params 参数数组
     * @param string $secret 密钥
     * @return string
     */
    protected static function generateSign(array $params, string $secret): string
    {
        // 过滤空值
        $params = array_filter($params, function($value) {
            return $value !== '' && $value !== null;
        });
        
        // 按key升序排序
        ksort($params);
        
        // 拼接字符串
        $string = '';
        foreach ($params as $key => $value) {
            $string .= $key . '=' . $value . '&';
        }
        $string = rtrim($string, '&');
        
        // 拼接密钥并生成签名
        $string .= '&app_secret='.$secret;

        // var_dump(md5($string));exit;

        // 使用md5加密（也可以使用sha1）
        return md5($string);
    }
}